package algorithm.tree;

/**
 * 建立二叉链表表示的完全二叉树
 */
public class CompleteBinaryTree<E> extends BinaryTree<E>  {
    //构造空二叉树
    public CompleteBinaryTree() {
        super();
    }
    //构造完全二叉树，levelorer指定完全二叉树的层次序列
    public CompleteBinaryTree(E[] levelorder) {
        this.root = create(levelorder,0);
    }
    //创建以levelorder[i]为根的一棵子树
    private BinaryNode create(E[] levelorder, int i) {
        BinaryNode<E> p = null;
        if(i < levelorder.length) {
            p = new BinaryNode<E>(levelorder[i]); //建立结点p
            p.left = create(levelorder, 2*i+1); //建立p的左子树
            p.right = create(levelorder, 2*i+2);//建立p的右子树
        }
        return p;
    }

    public static void main(String[] args) {
        String[] levelorder = {"A","B","C","D","E","F","G","H"};//完全二叉树的层次遍历序列
        CompleteBinaryTree<String> cbtree = new CompleteBinaryTree<String>(levelorder);
        cbtree.preOrder();
        cbtree.inOrder();
        cbtree.postOrder();
        System.out.println("结点个数："+cbtree.count());
        System.out.println("高度："+cbtree.height());
    }
}
